<template>
  <div>
    <Spin :spinning="loading" tip="Loading...">
      <div style="padding-top: 1rem;"></div>
      <!-- table -->
      <FormPanel ref="formPanel" :list="formList1" :cols="1" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
      <div style="padding-top: 1rem;"></div>
      <div
        :style="{
          position: 'absolute',
          position: 'absolute',
          left: 0,
          right: 0,
          zIndex: 9,
          borderTop: '1px solid #e9e9e9',
          padding: '10px 20px',
          background: '#fff',
          textAlign: 'right'
        }"
      >
        <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button>
        <multiuse-button @click="closeDialog">关 闭</multiuse-button>
      </div>
    </Spin>
  </div>
</template>
<script>
import {dictionary} from'@/utils/dictMixin';
import { isEmpty } from '@/utils/utils';
import { getThreeInfoList, newSave } from '@/api/repair/threePacksOfManager/warningParameterManagementCDuan';
export default {
  mixins: [dictionary],
  components: {

  },
  data() {
    return {
      loading: false,
      dataList1: [],
      visible: false,
      visibleDrawer: false,
      collapse: false,
      rowDate: {},
      infoId: null,
      flterValue: {}, //参数
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      formList1: this.createFormList1(),
      saveData: null,
    };
  },
  created() {
    //this.chuqi();
    this.getThreeNoStr();

  },
  methods: {
    formChangeHandle(val) {
      console.log(val);
      this.saveData = val;
    },
    //保存
    async saveDialog() {
      debugger;
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        this.loading = true;
        let redV=this.formList1.find(x => x.fieldName == 'red').initialValue;
        let yellowV=this.formList1.find(x => x.fieldName == 'yellow').initialValue;
        let orangeV=this.formList1.find(x => x.fieldName == 'orange').initialValue;
        let red = isEmpty(redV) ? '0' : `${redV}`;
        let yellow = isEmpty(yellowV) ? '0' : `${yellowV}`;
        let orange = isEmpty(orangeV) ? '0' : `${orangeV}`;
        let regExp = /(^[0-9]\d*$)/;

        if (!regExp.test(red)) {
          this.$notify.warning({ title: '提示信息', message: '红色预警数值只能是整数' });
          this.loading = false;
          return;
        }
        if (!regExp.test(orange)) {
          this.$notify.warning({ title: '提示信息', message: '橙色预警数值只能是整数' });
          this.loading = false;
          return;
        }
        if (!regExp.test(yellow)) {
          this.$notify.warning({ title: '提示信息', message: '黄色预警数值只能是整数' });
          this.loading = false;
          return;
        }
        if (
          parseFloat(red) < parseFloat(yellow) ||
          parseFloat(red) == parseFloat(yellow) ||
          parseFloat(red) < parseFloat(orange) ||
          parseFloat(red) == parseFloat(orange) ||
          parseFloat(orange) < parseFloat(yellow) ||
          parseFloat(orange) == parseFloat(yellow)
        ) {
          this.$notify.warning({ title: '提示信息', message: '预警级别的优先级为红>橙>黄' });
          this.loading = false;
          return;
        }

        let res = await newSave(this.saveData);
        console.log(res);
        if (res.resultCode === 200) {
          this.$emit('save');
          return this.$notify.success({title: '提示信息', message: '保存成功'});
        } else {
          this.loading = false;
          // this.$notify.error({title: '提示信息', message: res.errMsg || '保存失败!'});
          // this.closeDialog();
        }
      }
    },
    //取消操作
    closeDialog() {
      this.$emit('close', {visible: false, data: this.selectedRows, type: 'close'});
    },
    add() {
    },
    async chuqi() {

    },
    chooseOrderNo() {
      this.visible = true;
    },
    close() {
      this.visible = false;
    },
    //预警规则下拉
    async getThreeNoStr() {

      const res1 = await getThreeInfoList();
      if (res1.resultCode == 200) {

        this.formList1.find(item => item.fieldName == 'threeNo').itemList = res1.data.map(y => ({ text: y.value, value: y.value }));

      }
    },
    async changeThreeNoSelect(threeNo) {
      if (threeNo) {
        const res = await getThreeInfoList();
        if (res.resultCode === 200) {
          let mapdata= res.data;
          mapdata.forEach(val => {
            if (val.text == threeNo) {
              this.formList1.find(x => x.fieldName == 'threeName').initialValue=val.name;
            }
          });
        }
      }
    },
    createFormList1() {
      return [
        {
          type: 'SELECT',
          label: '规则编号',
          placeholder: '请选择...',
          fieldName: 'threeNo',
          rules: [{ required: true, message: '请选择...', trigger: 'change' }],
          itemList:[],
          change: val => {
            this.changeThreeNoSelect(val);
          }
        },
        {
          type: 'INPUT',
          label: '规则名称',
          disabled: true,// 只读
          readonly: true,// 只读
          fieldName: 'threeName',
          rules: [{ required: true, message: '规则名称...', trigger: 'blur' }],
        },

        {
          type: 'INPUT',
          label: '黄色预警',
          fieldName: 'yellow',
          placeholder: '黄色预警...',
          editType: 'number',
          rules: [{ required: true, message: '请输入黄色预警参数...', trigger: 'blur' }],
          offsetRightCols: 1
        },
        {
          type: 'INPUT',
          label: '橙色预警',
          fieldName: 'orange',
          placeholder: '橙色预警...',
          editType: 'number',
          rules: [{ required: true, message: '请输入橙色预警参数...', trigger: 'blur' }],
          offsetRightCols: 1
        },
        {
          type: 'INPUT',
          label: '红色预警',
          fieldName: 'red',
          placeholder: '红色预警...',
          editType: 'number',
          rules: [{ required: true, message: '请输入红色预警参数...', trigger: 'blur' }],
          offsetRightCols: 1
        },
        {
          type: 'SELECT',
          label: '状态',
          placeholder: '请选择...',
          fieldName: 'status',
          rules: [{ required: true, message: '请选择...', trigger: 'blur' }],
          initialValue:10011001,
          itemList: this.createDictList('1001')
        },
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
